<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/22
 * Time: 16:58
 */

namespace app\zucai\home;
use app\index\controller\Home;
use think\Db;

/**
 * Class Put
 * @package app\zucai\home
 * 充值与提现接口
 */

class Put extends Home
{
    /**
     * 充值订单提交接口
     */
  public function index(){
      if($this->request->isPost()){
          if($this->request->post()){
              $post=$this->request->post();

              if (!(isset($post['money']))) return returnError('未获取到充值金额');
              if (!(isset($post['uid']))) return returnError('未获取到用户id');
              if (!(isset($post['type']))) return returnError('未获取到充值方式');
              $order_id = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT).$post['uid'];
              if($post['type']=='weixin'){
                  $post['type']='微信';
              }elseif ($post['type']='alipay'){
                  $post['type']='支付宝';
              }
             $array=[
                'uid'=>$post['uid'],
                  'type'=>$post['type'],
                  'money'=>$post['money'],
                  'order_id'=>$order_id,
                  'time'=>date('Y-m-d H:i:s'),
                  'status'=>0,
              ];
             $con= Db::name('user_put')->insertGetId($array);
             $con= Db::name('user_put')->field('order_id')->where('id',$con)->find();
              if($con['order_id']){
                  return returnOk($con['order_id']);
              }else{
                  return returnError('充值申请未成功！');
              }
          }else{
              return returnError('未获取到参数！');
          }
      }else{
          return returnError('请使用POST方式请求');
      }
  }

  /**
   * 充值成功记录接口
   */
  public function put(){
      if($this->request->isPost()){
          if($this->request->post()){
              $post=$this->request->post();

              if (!(isset($post['order_id']))) return returnError('未获取到充值订单id');
              if (!(isset($post['money']))) return returnError('未获取到充值金额');
              $id=Db::name('user_put')->field('uid,status')->where([ 'order_id' => $post['order_id']])->find();
              if($id['status']==1){
                  return returnError('该订单已处理！');
              }
              //开启事务
              Db::startTrans();
              try {
                  $con = Db::name('user_put')->where([ 'order_id' => $post['order_id']])->update(['status' => 1,'money'=>$post['money']]);

                  $user = Db::name('user')->where('id', $id['uid'])->find();
                  $array = [
                      'order_id' => $post['order_id'],
                      'uid' => $id['uid'],
                      'type' => 3,
                      'caicontent'=>'充值',
                      'username' => $user['user'],
                      'name' => $user['name'],
                      'money' => $user['money'],
                      'lastmoney' => $user['money'] + $post['money'],
                      'dmoney' => $user['blockmoney'],
                      'time' => date('Y-m-d H:i:s'),
                  ];
                  Db::name('user')->where('id',$id['uid'])->setInc('money', $post['money']);

                  Db::name('account')->insert($array);

                      Db::commit();  // 提交事务
                      return returnOk($con);

              }catch (\Exception $e) {
                  Db::rollback();   // 回滚事务
                  return returnError('充值申请未成功！');
              }

          }else{
              return returnError('未获取到参数！');
          }
      }else{
          return returnError('请使用POST方式请求');
      }
  }
}